package gui;

import generated.business.room.RoomServicePackage.NullWorldException;
import generated.domain.message.Message;
import gui.perspective.AdminPerspective;
import gui.perspective.ChatRoomPerspective;
import gui.perspective.WorldsPerspective;
import gui.view.GlobalChatView;
import gui.view.RoomView;
import gui.wizard.StartWizard;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import vworld.Client;
import webservices.MessageWSClient;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void postWindowCreate() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(500, 300));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		configurer.setTitle("Welcome to VWorld");

		boolean connected = authenticate(Display.getCurrent());

		if (connected) {
			IWorkbenchWindow window = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow();
			IWorkbenchPage page = window.getActivePage();
			page.closeAllEditors(false);
			page.closePerspective(page.getPerspective(), false, false);

			if (Client.CONNECTED_USER.role.equals("administrator")) {// check if
				// user
				// is an
				// administrator
				try {// open admin perspective
					PlatformUI.getWorkbench().showPerspective(
							AdminPerspective.ID,
							PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow());
				} catch (WorkbenchException e) {
					e.printStackTrace();
				}
			} else if (!Client.CONNECTED_USER.role.equals("administrator")
					&& Client.USER_SERVICE.getAvatar(Client.CONNECTED_USER).name
							.equals("")) { // check if the simple user has
				// already an avatar
				// start creation avatar wizard
				StartWizard startWizard = new StartWizard();
				WizardDialog dlg = new WizardDialog(configurer.getWindow()
						.getShell(), startWizard);
				dlg.setBlockOnOpen(true);
				dlg.open();

				try {
					PlatformUI.getWorkbench().showPerspective(
							WorldsPerspective.ID,
							PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow());
				} catch (WorkbenchException e) {
					e.printStackTrace();
				}
			} else if (!Client.AVATAR_SERVICE.getRoomNameByAvatarName(
					Client.USER_SERVICE.getAvatar(Client.CONNECTED_USER).name)
					.equals("")) {
				// check if the simple user was already in a room when he last
				// disconnect
				try {
					String roomName = Client.AVATAR_SERVICE
							.getRoomNameByAvatarName(Client.USER_SERVICE
									.getAvatar(Client.CONNECTED_USER).name);
					try {
						Client.CURRENT_WORLD = Client.ROOM_SERVICE
								.getWorldByRoomName(roomName);
					} catch (NullWorldException e) {
						MessageDialog.openError(Display.getCurrent()
								.getActiveShell(), "Error", e.reason);
						PlatformUI.getWorkbench().restart();
						e.printStackTrace();
					}
					Client.CURRENT_ROOM = Client.ROOM_SERVICE
							.getRoomByName(roomName);
					Client.CALL_BACK_REF.roomName(roomName);

					Client.CONNECTED_USER.avatar.state = true;
					Client.CONNECTED_USER = Client.ROOM_SERVICE
							.notifyRoomOnLoginOrLogout(Client.CALL_BACK_REF,
									Client.CONNECTED_USER, true);

					// update client ref in server
					Client.CONNECTION_SERVICE.registerClient(
							Client.CONNECTED_USER.login, Client.CALL_BACK_REF);

					PlatformUI.getWorkbench().showPerspective(
							ChatRoomPerspective.ID,
							PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow());

					updateGlobalChatView(roomName);
					updateRoomView(roomName);
				} catch (WorkbenchException e) {
					e.printStackTrace();
				}
			} else if (Client.CONNECTED_USER.role.equals("simple user")) {
				try {
					PlatformUI.getWorkbench().showPerspective(
							WorldsPerspective.ID,
							PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow());
				} catch (WorkbenchException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void postWindowOpen() {
		/*Message[] messages = Client.MESSAGE_SERVICE
				.retreiveMessage(Client.CONNECTED_USER.avatar.name);*/
		Message[] messages = MessageWSClient.getRetrieveMessage(Client.CONNECTED_USER.avatar.name);
		for (Message m : messages) {
			String title = "New message from " + m.sender;
			String content = m.sender
					+ " sent you a message while you were disconnected from the room.\n"
					+ "Date : " + m.date + "\n" + m.content;
			MessageDialog.openInformation(
					Display.getCurrent().getActiveShell(), title, content);
		}
	}

	public boolean authenticate(Display display) {
		LoginDialog loginDialog = new LoginDialog(display);
		loginDialog.createContents();
		loginDialog.open();
		return true;
	}

	private void updateGlobalChatView(String roomName) {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IViewPart globalChatView = page.findViewReference(GlobalChatView.ID)
				.getView(false);

		if (globalChatView != null) {
			((GlobalChatView) globalChatView).changeTitle(roomName + " "
					+ "Global Chat");
			((GlobalChatView) globalChatView).getMessagesArea().setText("");
		}
	}

	private void updateRoomView(String roomName) {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IViewPart roomView = page.findViewReference(RoomView.ID).getView(false);

		if (roomView != null) {
			((RoomView) roomView).changeTitle(roomName);
		}
	}
}
